# -*- coding: iso-8859-2 -*-

import os
import re
import copy

from authorshipanalyzer.file import Path
from authorshipanalyzer.utils import sortDictionaryByValue
from authorshipanalyzer.experiments.AuthorData import AuthorData

class FeaturesExperiment:
    '''
    Tracking experiment of features importance
    '''
    
    def __init__(self):
        self.expRanks = {}
        self.expProc = {}
        self.author = ""
       
    def addResult(self, author, result, label):
        self.author = author
        
        sortedResult = sortDictionaryByValue(result)
        
        similarity = 0.0
        if result.has_key(author):
            similarity = result[author]  
            
        if not self.expProc.has_key(label):
            self.expProc[label] = []
        self.expProc[label].append(similarity)
        
        rank = 0
        for singleRes in sortedResult:
            rank += 1
            if singleRes[0] == author:
                break
            
        if not self.expRanks.has_key(label):
            self.expRanks[label] = []
        self.expRanks[label].append(rank)
    
    def printResults(self):
        '''
        Print result of comparison
        '''        
        # print procentage similarity result
        output = os.path.join(Path.getResourceDir(), "features", "features_proc.dat")
        outputFile = file(output, "w")
        
        for label, measures in self.expProc.items():
            sum = 0.0
            for measure in measures:
                sum += measure
            
            avgProc = 0.0
            if sum > 0.0:
                avgProc = sum / float(len(measures))
                
            outputFile.write("%s %f\n" % (label, avgProc) )
            
        outputFile.flush()
        outputFile.close()
        
        # print rank results
        output = os.path.join(Path.getResourceDir(), "features", "features_rank.dat")
        outputFile = file(output, "w")  
             
        for label, ranks in self.expRanks.items():   
                     
            sum = 0.0
            for rank in ranks:
                sum += rank
                
            avgRank = 0.0
            if sum > 0.0:
                avgRank = sum / float(len(ranks))
                
            outputFile.write("%s %f\n" % (label, avgRank) )      
            
        outputFile.flush()
        outputFile.close()
        
    def getFeaturesToCheck(self):
        expFeatures = {}
        
        f = {}
        f["features.document"] = True
        f["features.word"] = True
        f["features.char"] = True
        f["features.punctuation"] = True
        f["features.error"] = True
        f["features.email"] = True
        f["features.emailrel"] = True
        f["features.category"] = True
        expFeatures["ALL"] = copy.deepcopy(f)
        
        f["features.document"] = True
        f["features.word"] = False
        f["features.char"] = False
        f["features.punctuation"] = False
        f["features.error"] = False
        f["features.email"] = False
        f["features.emailrel"] = False
        f["features.category"] = False
        expFeatures["D"] = copy.deepcopy(f)
        
        f["features.document"] = False
        f["features.word"] = True
        f["features.char"] = False
        f["features.punctuation"] = False
        f["features.error"] = False
        f["features.email"] = False
        f["features.emailrel"] = False
        f["features.category"] = False
        expFeatures["W"] = copy.deepcopy(f)
        
        f["features.document"] = False
        f["features.word"] = False
        f["features.char"] = True
        f["features.punctuation"] = True
        f["features.error"] = False
        f["features.email"] = False
        f["features.emailrel"] = False
        f["features.category"] = False
        expFeatures["C"] = copy.deepcopy(f)
        
        f["features.document"] = False
        f["features.word"] = False
        f["features.char"] = False
        f["features.punctuation"] = False
        f["features.error"] = True
        f["features.email"] = False
        f["features.emailrel"] = False
        f["features.category"] = False
        expFeatures["B"] = copy.deepcopy(f)
        
        f["features.document"] = False
        f["features.word"] = False
        f["features.char"] = False
        f["features.punctuation"] = False
        f["features.error"] = False
        f["features.email"] = True
        f["features.emailrel"] = True
        f["features.category"] = False
        expFeatures["E"] = copy.deepcopy(f)
        
        f["features.document"] = False
        f["features.word"] = False
        f["features.char"] = False
        f["features.punctuation"] = False
        f["features.error"] = False
        f["features.email"] = False
        f["features.emailrel"] = False
        f["features.category"] = True
        expFeatures["S"] = copy.deepcopy(f)
                
        f["features.document"] = True
        f["features.word"] = True
        f["features.char"] = True
        f["features.punctuation"] = True
        f["features.error"] = False
        f["features.email"] = False
        f["features.emailrel"] = False
        f["features.category"] = False
        expFeatures["D+W+C"] = copy.deepcopy(f)
        
        f["features.document"] = True
        f["features.word"] = True
        f["features.char"] = True
        f["features.punctuation"] = True
        f["features.error"] = False
        f["features.email"] = True
        f["features.emailrel"] = True
        f["features.category"] = False
        expFeatures["D+W+C+E"] = copy.deepcopy(f)
        
        return expFeatures